package com.liyuanjinglyj.javacarddemo.slice;

import com.liyuanjinglyj.javacarddemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.webengine.ResourceRequest;
import ohos.agp.components.webengine.WebAgent;
import ohos.agp.components.webengine.WebConfig;
import ohos.agp.components.webengine.WebView;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class WebViewAbilitySlice extends AbilitySlice {
    HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
    private WebView webView;
    private static String EXAMPLE_URL;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_web_view);
        this.webView=(WebView)this.findComponentById(ResourceTable.Id_ability_web_view_webview);
        this.webView.getWebConfig().setJavaScriptPermit(true);
        if(intent != null) {
            EXAMPLE_URL = intent.getStringParam("url");
            this.webView.setWebAgent(new ExampleWebAgent());
            this.webView.load(EXAMPLE_URL);
        }
    }

    private class ExampleWebAgent extends WebAgent {
        @Override
        public boolean isNeedLoadUrl(WebView webview, ResourceRequest request) {
            Uri uri = request.getRequestUrl();
            if (EXAMPLE_URL.equals(uri.getDecodedHost())) {
                // 由WebView通过默认方式处理
                return false;
            }

            // 增加开发者自定义逻辑
            return super.isNeedLoadUrl(webview, request);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
